﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace InternshipManagement.Models
{
    public class SemesterRepository
    {
        public InternshipEntities entities = new InternshipEntities();

        public IQueryable<Semester> FindAllActiveSemesters()
        {
            return entities.Semesters.Where(s => (s.Status == "A" || s.Status == "C")).OrderByDescending(s => s.Status);
        }

        public Semester GetSemester(int id)
        {
            return entities.Semesters.FirstOrDefault(s => s.SemesterID == id);
        }

        public Semester GetActiveSemester()
        {
            return entities.Semesters.FirstOrDefault(s => s.Status == "C");
        }

        public bool SemesterExists(string Season,int Year)
        {
            return entities.Semesters.FirstOrDefault(s => s.Season == Season && s.Year == Year && s.Status != "P") != null;
        }

        public void AddSemester(Semester semester)
        {
            entities.Semesters.AddObject(semester);
        }

        public void DeleteSemester(Semester semester)
        {
            entities.Semesters.DeleteObject(semester);
        }

        //
        // Persistence
        public void Save()
        {
            entities.SaveChanges();
        }
    }
}